#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2020, Intel Corporation
#
#
# pmempool_sync_remote/TEST33 -- pmempool sync with remote replication
#                                and bad blocks in the regular file:
#                                - the pool consists of 2 parts
#                                - offset of bad block in all 2 parts: 1000
#

. ../unittest/unittest.sh

require_test_type medium
require_fs_type non-pmem
require_build_type debug nondebug
require_linked_with_ndctl $PMEMPOOL$EXESUFFIX

require_nodes 2
require_badblock_tests_enabled_node 0

setup

require_node_libfabric 0 $RPMEM_PROVIDER
require_node_libfabric 1 $RPMEM_PROVIDER

init_rpmem_on_node 0 1

export PMEMPOOL_LOG_LEVEL=10
export PMEMOBJ_LOG_LEVEL=10
export_vars_node 0 PMEMPOOL_LOG_LEVEL
export_vars_node 0 PMEMOBJ_LOG_LEVEL

require_node_log_files 0 pmemobj$UNITTEST_NUM.log
require_node_log_files 0 pmempool$UNITTEST_NUM.log

. ../common_badblock.sh

MOUNT_DIR="${NODE_DIR[0]}/mnt-pmem"
badblock_test_init_node 0 block_device $MOUNT_DIR

LAYOUT="layout"

LOG=out${UNITTEST_NUM}.log
rm -f $LOG && touch $LOG

POOLSET_LOCAL="local_pool.set"
POOLSET_REMOTE="remote_pool.set"

create_poolset $DIR/$POOLSET_LOCAL \
	8M:${MOUNT_DIR}/pool.local.part.0:x \
	8M:${MOUNT_DIR}/pool.local.part.1:x \
	M \
	${NODE_ADDR[1]}:$POOLSET_REMOTE

create_poolset $DIR/$POOLSET_REMOTE \
	16M:${NODE_DIR[1]}/pool.remote:x

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/$POOLSET_LOCAL
copy_files_to_node 1 ${NODE_DIR[1]} $DIR/$POOLSET_REMOTE

expect_normal_exit run_on_node 0 ../pmempool rm -sf ${NODE_DIR[0]}$POOLSET_LOCAL
expect_normal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT c v &>> $LOG"

###############################################################################
# Enabling features in poolsets with remote replicas is not supported now,
# so we have to use the following workaround to enable
# the POOL_FEAT_CHECK_BAD_BLOCKS compat feature in such poolset.
# We create the auxiliary poolset without the remote replica
# and enable this compat feature on all nodes separately.

POOLSET_LOCAL_AUX=${POOLSET_LOCAL}_aux

create_poolset $DIR/${POOLSET_LOCAL_AUX} \
	8M:${MOUNT_DIR}/pool.local.part.0:x \
	8M:${MOUNT_DIR}/pool.local.part.1:x

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/${POOLSET_LOCAL_AUX}

turn_on_checking_bad_blocks_node 0 ${NODE_DIR[0]}/${POOLSET_LOCAL_AUX}
turn_on_checking_bad_blocks_node 1 ${NODE_DIR[1]}/${POOLSET_REMOTE}

###############################################################################

# inject bad block in the part #0
FILE_0=${MOUNT_DIR}/pool.local.part.0
FIRST_SECTOR_0=$(expect_normal_exit run_on_node 0 ../extents $FILE_0 -l 1000)
ndctl_inject_error_node 0 $NAMESPACE $FIRST_SECTOR_0 1

# inject bad block in the part #1
FILE_1=${MOUNT_DIR}/pool.local.part.1
FIRST_SECTOR_1=$(expect_normal_exit run_on_node 0 ../extents $FILE_1 -l 1000)
ndctl_inject_error_node 0 $NAMESPACE $FIRST_SECTOR_1 1

expect_bad_blocks_node 0 $NAMESPACE

expect_abnormal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT v &>> $LOG"

expect_bad_blocks_node 0 $NAMESPACE

expect_normal_exit run_on_node 0 "../pmempool sync -b -v ${NODE_DIR[0]}$POOLSET_LOCAL &>> $LOG"
expect_normal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT v &>> $LOG"

ndctl_uninject_error_node 0 $FULLDEV $NAMESPACE $FIRST_SECTOR_0 1
ndctl_uninject_error_node 0 $FULLDEV $NAMESPACE $FIRST_SECTOR_1 1
badblock_test_fini_node 0 $MOUNT_DIR

pass
